package pers.xiaojun.boot.module.system.controller.permission;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import pers.xiaojun.boot.mybatis.core.convert.PageConvert;
import pers.xiaojun.boot.common.pojo.CommonResult;
import pers.xiaojun.boot.common.pojo.PageResult;
import pers.xiaojun.boot.common.util.BeanUtils;
import pers.xiaojun.boot.module.system.controller.permission.vo.role.RoleReqVO;
import pers.xiaojun.boot.module.system.controller.permission.vo.role.RoleRespVO;
import pers.xiaojun.boot.module.system.controller.permission.vo.role.RoleSaveVO;
import pers.xiaojun.boot.module.system.controller.permission.vo.role.RoleSimpleRespVO;
import pers.xiaojun.boot.module.system.dao.entity.permission.RoleDO;
import pers.xiaojun.boot.module.system.service.permission.RoleService;
import pers.xiaojun.boot.web.core.annotation.AdminApi;

import java.util.List;

/**
 * 角色控制层
 *
 * @author xiaojun
 * @since 2025-10-26
 */
@Tag(name = "角色")
@AdminApi
@RestController
@RequestMapping("/system/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @PostMapping()
    @PreAuthorize("@ss.hasPermission('system:role:create')")
    @Operation(summary = "创建角色")
    public CommonResult<?> createRole(@RequestBody @Valid RoleSaveVO createVO) {
        return CommonResult.success(roleService.saveRole(createVO).getId());
    }

    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:role:update')")
    @Operation(summary = "更新角色")
    @Parameter(name = "id", description = "角色Id", required = true, example = "1")
    public CommonResult<?> updateRole(@PathVariable("id") Long roleId, @RequestBody @Valid RoleSaveVO updateVO) {
        updateVO.setId(roleId);
        roleService.updateRole(updateVO);
        return CommonResult.success();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:role:delete')")
    @Operation(summary = "删除角色")
    @Parameter(name = "id", description = "角色Id", required = true, example = "1")
    public CommonResult<?> deleteRole(@PathVariable("id") Long roleId) {
        roleService.removeRoleById(roleId);
        return CommonResult.success();
    }

    @DeleteMapping("/batch/{ids}")
    @PreAuthorize("@ss.hasPermission('system:role:delete')")
    @Operation(summary = "删除一批角色")
    @Parameter(name = "ids", description = "角色Id列表（用,号分隔）", required = true, example = "1,2")
    public CommonResult<?> deleteBatchRole(@PathVariable("ids") List<Long> roleIds) {
        roleService.removeRoleByIds(roleIds);
        return CommonResult.success();
    }


    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:role:query')")
    @Operation(summary = "获取角色")
    @Parameter(name = "id", description = "角色Id", required = true, example = "1")
    public CommonResult<RoleRespVO> getRole(@PathVariable("id") Long roleId) {
        return CommonResult.success(roleService.getRoleVOById(roleId));
    }

    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:role:query')")
    @Operation(summary = "获取角色分页")
    public CommonResult<PageResult<RoleRespVO>> getRolePage(@Valid RoleReqVO roleReqVO) {
        Page<RoleDO> rolePage = roleService.listRolePage(roleReqVO);
        return CommonResult.success(PageConvert.INSTANCE.toPageResult(rolePage, RoleRespVO.class));
    }

    @GetMapping("/simple-list")
    @PreAuthorize("@ss.hasPermission('system:role:query')")
    @Operation(summary = "获取角色简要信息列表")
    public CommonResult<List<RoleSimpleRespVO>> getSimpleRoleList() {
        return CommonResult.success(BeanUtils.toBeans(roleService.list(), RoleSimpleRespVO.class));
    }

}
